﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using System.Text;
using He.Common;
using System.Data;

namespace He.Web.manage.weixin
{
    public partial class window_articles : Manage
    {
        protected int pageSize = 10;                          //每页记录数
        protected int page;                                   //当前页
        protected int pcount;                                 //记录总数
        protected int category_id;                            //类别ID
        protected string property;                            //属性
        protected string keywords;                            //关键字
        protected void Page_Load(object sender, EventArgs e)
        {
            page = GetInt("page", 0);
            category_id = GetInt("category_id", 0);
            property = GetString("property");
            keywords = GetString("keywords");

            siteinfo = new BLL.site_info().GetModelByLang(CurrentLang);
            if (!IsPostBack)
            {
                ddlCategoryBind();

                RptBind();
            }
        }

        #region 数据绑定
        private void RptBind()
        {
            if (this.category_id > 0)
            {
                this.ddlcategory_id.SelectedValue = this.category_id.ToString();
            }
            this.txtKeywords.Text = this.keywords;
            this.ddlProperty.SelectedValue = this.property;
            string strWhere = Comb_SqlTxt(this.category_id, this.keywords, this.property);
            BLL.article bll = new BLL.article();
            rptList.DataSource = bll.GetPageList(pageSize, page, strWhere, "sort_id asc,id desc");
            rptList.DataBind();
            pcount = bll.GetRecordCount(strWhere);
        }
        #endregion

        #region 组合SQL语句
        private string Comb_SqlTxt(int _category_id, string _keywords, string _property)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            strTemp.Append("id>0 ");
            if (_category_id > 0)
            {
                strTemp.Append(" and category_id in(select Id from he_category where id_list like '%," + _category_id + ",%')");
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and title like '%" + _keywords + "%'");
            }
            if (!string.IsNullOrEmpty(_property))
            {
                switch (_property)
                {
                    case "isLock":
                        strTemp.Append(" and is_lock=1");
                        break;
                    case "unIsLock":
                        strTemp.Append(" and is_lock=0");
                        break;
                    case "isMsg":
                        strTemp.Append(" and is_msg=1");
                        break;
                    case "isTop":
                        strTemp.Append(" and is_top=1");
                        break;
                    case "isRed":
                        strTemp.Append(" and is_red=1");
                        break;
                    case "isHot":
                        strTemp.Append(" and is_hot=1");
                        break;
                    case "isImg":
                        strTemp.Append(" and is_img=1");
                        break;
                }
            }

            return strTemp.ToString();
        }
        #endregion

        #region 类别绑定
        private void ddlCategoryBind()
        {
            List<Model.Lang> lst = new BLL.config().LoadConfig<List<Model.Lang>>(LangXmlPath);

            string lang_list = "";
            foreach (Model.Lang item in lst)
            {
                if (item.IsShow)
                {
                    lang_list = lang_list + "," + item.Value + ",";
                }
            }

            BLL.channel sbll = new BLL.channel();
            BLL.category cbll = new BLL.category();
            //绑定类别
            DataTable he_sys = sbll.GetList("is_lock=0 and lang='" + CurrentLang + "'").Tables[0];
            ddlcategory_id.Items.Clear();
            ddlcategory_id.Items.Add(new ListItem("所有分类", ""));
            foreach (DataRow dr_sys in he_sys.Rows)
            {
                DataTable he_category = cbll.GetList(0, Convert.ToInt32(dr_sys["id"]));
                foreach (DataRow dr_category in he_category.Rows)
                {
                    if (lang_list.Contains("," + dr_sys["lang"].ToString() + ","))
                    {
                        string Id = dr_category["id"].ToString();
                        int layer = int.Parse(dr_category["layer"].ToString());
                        string Title = dr_category["title"].ToString().Trim();

                        if (layer == 1)
                        {
                            Title = string.Format("【{0}】{1}", dr_sys["title"], Title);
                            ddlcategory_id.Items.Add(new ListItem(Title, Id));
                        }
                        else
                        {
                            Title = "├ " + Title;
                            Title = StringHelper.Repeat("　", layer - 1) + Title;
                            ddlcategory_id.Items.Add(new ListItem(Title, Id));
                        }
                    }
                }
            }
        }
        #endregion

        //类别
        protected void ddlcategory_id_SelectedIndexChanged(object sender, EventArgs e)
        {
            int _category_id;
            if (int.TryParse(this.ddlcategory_id.SelectedValue.ToString(), out _category_id))
            {
                Response.Redirect("window_articles.aspx?" + CombUrlTxt(0, _category_id, this.keywords, this.property) + "iskeyword=" + Request["iskeyword"]);
            }
            else
            {
                Response.Redirect("window_articles.aspx?" + CombUrlTxt(0, 0, this.keywords, this.property) + "iskeyword=" + Request["iskeyword"]);
            }
        }
        //属性
        protected void ddlProperty_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("window_articles.aspx?" + CombUrlTxt(0, this.category_id, this.keywords, this.ddlProperty.SelectedValue) + "iskeyword=" + Request["iskeyword"]);
        }
        //搜索
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Response.Redirect("window_articles.aspx?" + CombUrlTxt(0, this.category_id, txtKeywords.Text.Trim(), this.property) + "iskeyword=" + Request["iskeyword"]);
        }
    }
}